package com.seth.myapplication;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.seth.myapplication.bean.User;
import com.seth.myapplication.helper.SetStatusTabHeight;
import com.seth.myapplication.login.LoginPage;
import com.seth.myapplication.service.NetWorkService;
import com.seth.myapplication.surface.page4Fragment;
import com.seth.myapplication.utils.SqlDao;

public class LoginActivity extends FragmentActivity {
    public static final String LOGIN_REGISTER = "com.seth.myapplication.service.RECEIVER";
    private LinearLayout topStatus;


    private MsgReceiver msgReceiver;
    private static Fragment contentFragment;

    private FragmentManager fm;
    private User mUser;
    public static void setContentFragment(Fragment fragment) {
        contentFragment = fragment;
    }



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);

        setContentView(R.layout.activity_login);


        //动态注册广播接收器
        msgReceiver = new MsgReceiver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(LOGIN_REGISTER);
        registerReceiver(msgReceiver, intentFilter);


        initView();
        initEvent();

        fm = getSupportFragmentManager();

        contentFragment = fm.findFragmentById(R.id.activity_login);

        if(contentFragment == null){
            contentFragment = new LoginPage();
            fm.beginTransaction().add(R.id.activity_login, contentFragment).commit();

        }

    }

    private void initEvent() {
        new SetStatusTabHeight(topStatus,this).setRelativeLayout();
    }

    private void initView() {
        topStatus = (LinearLayout) findViewById(R.id.login_top_sts);

    }
    /**
     * 广播接收器
     * @author len
     *
     */
    public class MsgReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            //拿到进度，更新UI
            int result = intent.getIntExtra("result", 0);
            if(result==1){
                Intent i = intent;
                User user = (User) i.getSerializableExtra("usrInfo");
                boolean rs = new SqlDao(getBaseContext()).addUser(user);
                Log.i("SQLiteresult",rs+"");
                Intent backIntent = new Intent();
                backIntent.putExtra("user",user);
                Log.i("okhttpRespond",user.getNickname());
                LoginActivity.this.setResult(4,backIntent);
                if(!user.getFrog().equals("images\\\\default.jpg")){
                    new NetWorkService(LoginActivity.this, page4Fragment.UPDATE_NET_FROG).downloadImage(user.getFrog(),user.getUsrId());
                }
                finish();
            }else if(result==2){
                Toast.makeText(context, "密码错误", Toast.LENGTH_SHORT).show();
            }else if(result==3){
                Toast.makeText(context, "账号不存在", Toast.LENGTH_SHORT).show();
            }else if(result==4){
                Intent i = intent;
                User user = (User) i.getSerializableExtra("usrInfo");
                Intent backIntent = new Intent();
                backIntent.putExtra("user",user);
                LoginActivity.this.setResult(4,backIntent);
                finish();
            }else if(result==5){
                Toast.makeText(context, "手机号已注册", Toast.LENGTH_SHORT).show();
            }else if(result==6){
                Toast.makeText(context, "注册失败", Toast.LENGTH_SHORT).show();
            }
        }

    }

    @Override
    public void onDestroy(){
        unregisterReceiver(msgReceiver);
        super.onDestroy();
    }


}
